package com.yitongmao.bottompop;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.yitongmao.yitmfruit.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 城市Picker
 * @author zd
 */
public class CityPicker extends LinearLayout {
    /**
     * 滑动控件
     */
    private ScrollerNumberPicker provincePicker;
    private ScrollerNumberPicker cityPicker;
    private ScrollerNumberPicker counyPicker;
    /**
     * 选择监听
     */
    private OnSelectingListener onSelectingListener;
    /**
     * 刷新界面
     */
    private static final int REFRESH_VIEW = 0x001;
    /**
     * 临时地址
     */
    private int tempProvinceIndex = 0;
    private int temCityIndex = 0;
    private int tempCounyIndex = 0;
    private Context context;
    private List<Cityinfo> province_list = new ArrayList<>();
    private HashMap<String, List<Cityinfo>> city_map = new HashMap<String, List<Cityinfo>>();
    private HashMap<String, List<Cityinfo>> couny_map = new HashMap<String, List<Cityinfo>>();
    private CitycodeUtil citycodeUtil;
    private String city_string;
    private String provicecode = "110000";
    private String citycode = "110100";
    private String city_code_string = "110102";

    public CityPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        getaddressinfo();
    }
    public CityPicker(Context context) {
        super(context);
        this.context = context;
        citycodeUtil = CitycodeUtil.getSingleton();
        getaddressinfo();
    }
    // 获取城市信息
    private void getaddressinfo() {
        // 读取城市信息string
        province_list = CityUtil2.getProvinces();
        city_map = CityUtil2.getCitys();
        couny_map = CityUtil2.getCountrys();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater.from(getContext()).inflate(R.layout.city_picker, this);
        citycodeUtil = CitycodeUtil.getSingleton();
        // 获取控件引用
        provincePicker = (ScrollerNumberPicker) findViewById(R.id.province);

        cityPicker = (ScrollerNumberPicker) findViewById(R.id.city);
        counyPicker = (ScrollerNumberPicker) findViewById(R.id.couny);
        provincePicker.setData(citycodeUtil.getProvince(province_list));
        provincePicker.setDefault(tempProvinceIndex);
        provicecode = citycodeUtil.getProvince_list_code().get(tempProvinceIndex);

        cityPicker.setData(citycodeUtil.getCity(city_map, citycodeUtil
                .getProvince_list_code().get(temCityIndex)));
        cityPicker.setDefault(temCityIndex);
        citycode = citycodeUtil.getCity_list_code().get(temCityIndex);

        counyPicker.setData(citycodeUtil.getCouny(couny_map, citycodeUtil
                .getCity_list_code().get(tempCounyIndex)));
        counyPicker.setDefault(tempCounyIndex + 1);
        city_code_string = citycodeUtil.getCouny_list_code().get(tempCounyIndex + 1);

//		省级监听
        provincePicker.setOnSelectListener(new ScrollerNumberPicker.OnSelectListener() {

            @Override
            public void endSelect(int id, String text) {
//                选择完毕
                isProvinceSelect = true;

                if (TextUtils.isEmpty(text))
                    return;
//                if (tempProvinceIndex != id) {
                String selectDay = cityPicker.getSelectedText();
                if (selectDay == null || selectDay.equals(""))
                    return;
                String selectMonth = counyPicker.getSelectedText();
                if (selectMonth == null || selectMonth.equals(""))
                    return;
                // 城市数组
                provicecode = citycodeUtil.getProvince_list_code().get(id);
                cityPicker.setData(citycodeUtil.getCity(city_map,
                        provicecode));
                cityPicker.setDefault(0);
                citycode = citycodeUtil.getCity_list_code().get(0);
                counyPicker.setData(citycodeUtil.getCouny(couny_map,
                        citycodeUtil.getCity_list_code().get(0)));
                counyPicker.setDefault(0);
                city_code_string = citycodeUtil.getCouny_list_code()
                        .get(0);
                int lastDay = Integer.valueOf(provincePicker.getListSize());
                if (id > lastDay) {
                    provincePicker.setDefault(lastDay - 1);
                }
//                }
                tempProvinceIndex = id;
                Message message = new Message();
                message.what = REFRESH_VIEW;
                handler.sendMessage(message);
            }

            @Override
            public void selecting(int id, String text) {
                isProvinceSelect = false;
            }
        });
//		市级监听
        cityPicker.setOnSelectListener(new ScrollerNumberPicker.OnSelectListener() {

            @Override
            public void endSelect(int id, String text) {
                isCitySelect = true;
                if (TextUtils.isEmpty(text))
                    return;
                if (temCityIndex != id) {
                    String selectDay = provincePicker.getSelectedText();
                    if (selectDay == null || selectDay.equals(""))
                        return;
                    String selectMonth = counyPicker.getSelectedText();
                    if (selectMonth == null || selectMonth.equals(""))
                        return;
                    citycode = citycodeUtil.getCity_list_code().get(id);
                    counyPicker.setData(citycodeUtil.getCouny(couny_map,
                            citycode));
                    counyPicker.setDefault(0);
                    city_code_string = citycodeUtil.getCouny_list_code()
                            .get(0);
                    int lastDay = Integer.valueOf(cityPicker.getListSize());
                    if (id > lastDay) {
                        cityPicker.setDefault(lastDay - 1);
                    }
                }
                temCityIndex = id;
                Message message = new Message();
                message.what = REFRESH_VIEW;
                handler.sendMessage(message);
            }

            @Override
            public void selecting(int id, String text) {
                isCitySelect = false;

            }
        });
//		县城监听
        counyPicker.setOnSelectListener(new ScrollerNumberPicker.OnSelectListener() {

            @Override
            public void endSelect(int id, String text) {
                isCounySelect = true;
                if (TextUtils.isEmpty(text))
                    return;
                if (tempCounyIndex != id) {
                    String selectDay = provincePicker.getSelectedText();
                    if (selectDay == null || selectDay.equals(""))
                        return;
                    String selectMonth = cityPicker.getSelectedText();
                    if (selectMonth == null || selectMonth.equals(""))
                        return;
                    // 城市数组
                    city_code_string = citycodeUtil.getCouny_list_code()
                            .get(id);
                    int lastDay = Integer.valueOf(counyPicker.getListSize());
                    if (id > lastDay) {
                        counyPicker.setDefault(lastDay - 1);
                    }
                }
                tempCounyIndex = id;
                Message message = new Message();
                message.what = REFRESH_VIEW;
                handler.sendMessage(message);
            }

            @Override
            public void selecting(int id, String text) {
                isCounySelect = false;
            }
        });
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case REFRESH_VIEW:
                    if (onSelectingListener != null)
                        onSelectingListener.selected(true);
                    break;
                default:
                    break;
            }
        }
    };

    public void setOnSelectingListener(OnSelectingListener onSelectingListener) {
        this.onSelectingListener = onSelectingListener;
    }

    public String getCity_code_string() {
        return city_code_string;
    }


    public interface OnSelectingListener {

        public void selected(boolean selected);
    }

    /**
     * 获取选择的城市信息
     *
     * @return
     */
    public String getCity_string() {
        city_string = provincePicker.getSelectedText() + "#"
                + cityPicker.getSelectedText() + "#" + counyPicker.getSelectedText();
        return city_string;
    }


    public String getProvince() {
        return provincePicker.getSelectedText();
    }

    public String getCity() {
        return cityPicker.getSelectedText();
    }

    public String getCouny() {
        return counyPicker.getSelectedText();
    }

    public String getProvicecode() {
        return provicecode;
    }

    public String getCitycode() {
        return citycode;
    }

    public String getCountryCode() {
        return city_code_string;
    }

    /**
     * 省市县 选择完毕的判断
     */
    public boolean isProvinceSelect;
    public boolean isCitySelect;
    public boolean isCounySelect;

}
